package com.zjdiepu.www.biz.yw;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.yw.YwBusinessCarAreaPriceMapper;
import com.zjdiepu.www.ibiz.yw.IYwBusinessCarAreaPriceService;
import com.zjdiepu.www.model.yw.dto.YwBusinessCarAreaPrice;
import com.zjdiepu.www.model.yw.ext.YwBusinessCarAreaPriceExt;

/**
 * 二手车区域估价Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年04月24日 14时45分<br/>
 * @since 1.0<br/>
 */
@Service
public class YwBusinessCarAreaPriceServiceImpl extends BaseService implements IYwBusinessCarAreaPriceService {
	public static final Logger logger = LoggerFactory.getLogger(YwBusinessCarAreaPriceServiceImpl.class);

	@Autowired
	private YwBusinessCarAreaPriceMapper ywBusinessCarAreaPriceMapper;
	
	@Override
	public void saveUpdateAndDelete(YwBusinessCarAreaPrice ywBusinessCarAreaPrice, OperateType operType) {
		switch (operType) {
		case add:
			ywBusinessCarAreaPrice.initInsertData(ywBusinessCarAreaPrice.getModifier(), ywBusinessCarAreaPrice.getModifierName());
			ywBusinessCarAreaPriceMapper.insert(ywBusinessCarAreaPrice);
			break;
		case edit:
			ywBusinessCarAreaPrice.initUpdateData(ywBusinessCarAreaPrice.getModifier(), ywBusinessCarAreaPrice.getModifierName());
			// 影响的记录数
			int updateCount = ywBusinessCarAreaPriceMapper.updateByPrimaryKeySelective(ywBusinessCarAreaPrice);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			ywBusinessCarAreaPrice.initDeleteData(ywBusinessCarAreaPrice.getModifier(), ywBusinessCarAreaPrice.getModifierName());
			// 影响的记录数
			int deleteCount = ywBusinessCarAreaPriceMapper.updateByPrimaryKeySelective(ywBusinessCarAreaPrice);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<YwBusinessCarAreaPrice> selectByExt(YwBusinessCarAreaPriceExt ywBusinessCarAreaPriceExt) {
		return ywBusinessCarAreaPriceMapper.selectByExt(ywBusinessCarAreaPriceExt);
	}
	
	@Override
	public int countByExt(YwBusinessCarAreaPriceExt ywBusinessCarAreaPriceExt){
		return ywBusinessCarAreaPriceMapper.countByExt(ywBusinessCarAreaPriceExt);
	}
	
	@Override
	public YwBusinessCarAreaPrice selectById(String id) {
		return ywBusinessCarAreaPriceMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			ywBusinessCarAreaPriceMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(YwBusinessCarAreaPrice ywBusinessCarAreaPrice) {
		return ywBusinessCarAreaPriceMapper.countRefined(ywBusinessCarAreaPrice);
	}

	@Override
	public YwBusinessCarAreaPrice selectRefined(YwBusinessCarAreaPrice ywBusinessCarAreaPrice) {
		return ywBusinessCarAreaPriceMapper.selectRefined(ywBusinessCarAreaPrice);
	}

	@Override
	public boolean repeat(YwBusinessCarAreaPrice ywBusinessCarAreaPrice) {
		int count = countRefined(ywBusinessCarAreaPrice);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(ywBusinessCarAreaPrice.getId())) {
			return true;
		}
		YwBusinessCarAreaPrice dbItem = selectRefined(ywBusinessCarAreaPrice);
		if(null == dbItem) {
			return false;
		}
		return !ywBusinessCarAreaPrice.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return ywBusinessCarAreaPriceMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}

	@Override
	public int insertBatch(List<YwBusinessCarAreaPrice> ywBusinessCarAreaPrices) {
		return ywBusinessCarAreaPriceMapper.insertBatch(ywBusinessCarAreaPrices);
	}

	@Override
	public int deletedRefined(YwBusinessCarAreaPrice ywBusinessCarAreaPrice) {
		return ywBusinessCarAreaPriceMapper.deletedRefined(ywBusinessCarAreaPrice);
	}
}
